﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Gao.Poker.Domain;
using Gao.Poker.Services;

namespace Gao.Poker.ViewModel
{
    public class ConsumptionViewModel : BaseViewModel
    {
        private List<Consumption> _consumptions;
        private Consumption _consumption;
        private ConsumptionEx _consumptionEx;
        private Member _member;
        private String _cardNo;
        private String _barCode;
        private Product _product;
        public ObservableCollection<ProductHelper> ConsumptionProducts { get; set; }
        private Decimal _monetary;
        private Int32 _pageIndex;
        private int _consumptionTotal;

        public String MemberName { get; set; }
        public String MemberCardNo { get; set; }

        public ConsumptionViewModel()
        {
            MemberName = String.Empty;
            MemberCardNo = String.Empty;
            ConsumptionProducts = new ObservableCollection<ProductHelper>();
        }

        public List<Consumption> Consumptions
        {
            get
            {
                _consumptions = ConsumptionService.Instance().GetPageList(MemberName, MemberCardNo, PageIndex, 20);
                OnPropertyChanged("ConsumptionTotal");
                return _consumptions;
            }
            set { _consumptions = value; }
        }

        public int ConsumptionTotal
        {
            get
            {
                _consumptionTotal = ConsumptionService.Instance().GetTotal(MemberName, MemberCardNo);
                return _consumptionTotal;
            }
        }

        public Consumption Consumption
        {
            get { return _consumption; }
            set
            {
                _consumption = value;
                OnPropertyChanged("Consumption");
            }
        }

        public ConsumptionEx ConsumptionEx
        {
            get { return _consumptionEx; }
            set { _consumptionEx = value; }
        }

        public Member Member
        {
            get
            {
                _member = new MemberService().GetMember(CardNo);
                return _member;
            }
            set { _member = value; }
        }

        public String CardNo
        {
            get { return _cardNo; }
            set
            {
                _cardNo = value;
                OnPropertyChanged("Member");
            }
        }

        public String BarCode
        {
            get { return _barCode; }
            set
            {
                _barCode = value;
            }
        }

        public Decimal Monetary
        {
            get
            {
                _monetary = ConsumptionProducts.Sum(a => a.Total);
                return _monetary;
            }
            set { _monetary = value; }
        }

        public Product GetProduct()
        {
            _product = new ProductService().GetProduct(BarCode);
            if (_product != null)
            {
                var ph = new ProductHelper(_product);
                var tmp = ConsumptionProducts.FirstOrDefault(a => a.BarCode.Equals(ph.BarCode));
                if (tmp != null)
                {
                    tmp.Number++;
                }
                else
                {
                    tmp = ph;
                }
                ConsumptionProducts.Remove(tmp);
                ConsumptionProducts.Insert(0, tmp);
            }

            return _product;
        }

        public int PageIndex
        {
            get { return _pageIndex; }
            set
            {
                _pageIndex = value;
                OnPropertyChanged("Consumptions");
            }
        }

    }
}
